package com.lazy.utils.interceptor;

import com.lazy.utils.annotation.Access;
import com.lazy.utils.exception.BusinessException;
import com.lazy.utils.token.client.ClientTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @author: zk
 * @create_time: 2018-07-25 14:59
 * @describe:
 */
@Slf4j
@Configuration
@EnableAutoConfiguration
public class AccessInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    ClientTokenUtil clientTokenUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //解决跨域问题
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        response.setHeader("Access-Control-Allow-Headers"," Authentication,Origin, X-Requested-With, Content-Type, Accept,access_token");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Expose-Headers", "access_token");


        if (request.getMethod().equals("OPTIONS")) {
//            HttpUtil.setResponse(response, HttpStatus.OK.value(), null);
            response.setStatus(HttpStatus.OK.value());
//            response.setStatus(HttpStatus.SC_OK);
            return true;
        }

        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            //使用注解方式，验证token
            Method method = handlerMethod.getMethod();
            Access access = method.getAnnotation(Access.class);
            if (access != null && access.access()) {
                String token = request.getHeader("access_token");
                if(null == token){
                    throw new BusinessException("token无效");
                }else{
                    BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
                    ClientTokenUtil clientTokenUtil = factory.getBean(ClientTokenUtil.class);

                    try{
                        User user = clientTokenUtil.getInfoFromToken(token);
                        return true;
                    }catch (Exception e){
                        throw new BusinessException("token无效");
                    }
                }
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, ModelAndView modelAndView) throws Exception {
//        log.info("--------------处理请求完成后视图渲染之前的处理操作---------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler, Exception e) throws Exception {
//        log.info("---------------视图渲染之后的操作-------------------------0");
    }
}

